package com.cloud.autoconfigure;

import com.cloud.async.business.context.AbstractAsyncContext;
import com.cloud.async.business.context.support.StandardAsyncContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * 自动配置类
 *
 * @author chengrui
 */
@Configuration
@EnableConfigurationProperties(WebAsyncAutoBean.class)
@ConditionalOnBean({WebAsyncMarker.class, Executor.class})
public class WebAsyncAutoConfiguration {

    @Autowired(required = false)
    private Executor executor;

    @Bean
    @ConditionalOnMissingBean(AbstractAsyncContext.class)
    StandardAsyncContext simpleBean(WebAsyncAutoBean webAsyncAutoBean) {
        return new StandardAsyncContext.Builder()
                .executor(executor).dataCapacity(webAsyncAutoBean.getDataCapacity())
                .globalTimeout(webAsyncAutoBean.getGlobalTimeout())
                .resultHandler(webAsyncAutoBean.getResultHandler()).lifeHandler(webAsyncAutoBean.getErrorHandler(), webAsyncAutoBean.getResultHandlerMonitor())
                .retryPolicy(webAsyncAutoBean.getRetryPolicy()).build();
    }


}
